package com.cj.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cj.common.dto.PageDTO;
import com.cj.item.constants.MqConstants;
import com.cj.item.mapper.ItemMapper;
import com.cj.item.pojo.Item;
import com.cj.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Resource
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public PageDTO<Item> pageQuery(Integer page, Integer size) {
        IPage<Item> pageBean = new Page<>(page,size);

        IPage<Item> itemIPage = itemMapper.selectPage(pageBean, null);

        return new PageDTO<>(itemIPage.getTotal(),itemIPage.getRecords());
    }


    @Transactional
    public void updateItemById(Long id, Integer status) {

        this.update().set("status",status).eq("id",id).update();

        String key = status == 1 ? MqConstants.ITEM_INSERT_KEY : MqConstants.ITEM_DELETE_KEY;

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,key,id);
    }
}
